package cn.edu.lingnan.controller;

import cn.edu.lingnan.bean.Book;
import cn.edu.lingnan.bean.User;
import cn.edu.lingnan.service.user.UserService;
import cn.edu.lingnan.tools.ResultVo;
import cn.edu.lingnan.tools.ResultVoUtils;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.Session;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.Id;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @PostMapping("/userLogin")
    @ResponseBody
    public ResultVo userLogin(@RequestBody Map map, HttpSession session) {

        //System.out.println(map.toString() + "===========map");
        //System.out.println(map.get("userName") + "=======userName");
        //System.out.println(map.get("passWord") + "=======passWord");
        //System.out.println(map.get("captcha") + "=======captcha");

        String userName = (String) map.get("userName");
        String passWord = (String) map.get("passWord");
        String captcha = (String) map.get("captcha");


        String code = (String) session.getAttribute("code");

        System.out.println(code+"=====验证码");

        if (code.equalsIgnoreCase(captcha)) {

            //System.out.println("进来登录验证了吗");

            User login = userService.login(userName, passWord);

            // ** 添加session拿到id  session.setAttribute("loginUser_id",login.getId())

            session.setAttribute("loginUser",login);
            //session.setAttribute("loginUser_id",login.getId());

            if (ObjectUtil.isNotNull(login)) {
                return ResultVoUtils.success("登录成功", "登录验证操作");
            }else {
                return ResultVoUtils.error("账户名密码错误", "登录验证操作");
            }
        } else {
            return ResultVoUtils.error("验证码错误", "登录验证操作");
        }

    }




    //构造验证码
    @GetMapping("/getNumCode")
    public void getNumCode(HttpServletResponse response, HttpSession session) throws IOException, IOException {
        //定义图形验证码的长和宽，验证码字符数，和干扰线
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(120,40,4,20);
        //获取验证码
        session.setAttribute("code",captcha.getCode());
        System.out.println("验证码====>"+captcha.getCode());
        ServletOutputStream outputStream = response.getOutputStream();
        captcha.write(outputStream);
        outputStream.close();

    }

    @GetMapping("/showUser")
    public String showUser(Integer id, Model model){
        User user = userService.findUserById(id);
        model.addAttribute("user",user);
        return "bookpage/user-setting";
    }

    // ** 添加 password 方法

    @GetMapping("/password")
    public String Password(){
        return "bookpage/user-password";
    }

    // ** 添加 updatePassword 方法
    @PostMapping("/updatePassword")
    @ResponseBody
    public ResultVo updatePassword(@RequestBody Map map,HttpSession session){

        System.out.println(map);

       // Object data = map.get("field");

        String old_password = (String) map.get("old_password");
        String new_password = (String) map.get("new_password");
        String again_password = (String) map.get("again_password");

        //System.out.println(old_password);
        //System.out.println(new_password);
        //System.out.println(again_password);

        if (new_password.equals(again_password)){

            //System.out.println(session.getAttribute("loginUser")+"session传参");
            User user = (User) session.getAttribute("loginUser");
            Integer id = (Integer) session.getAttribute("loginUser_id");
           // User userById = userService.findUserById(id);
            if (user.getId() == id){
                user.setPassWord(new_password);
                userService.saveUser(user);
                return ResultVoUtils.success("修改密码成功！","修改密码操作");

            }else {
                return ResultVoUtils.error("旧密码不正确","修改密码操作");
            }

        }else {
            return ResultVoUtils.error("两次输入密码不相同","修改密码操作");
        }




    }



}
